<?php
declare (strict_types=1);

namespace app\api\controller;

use app\api\business\GoodsBusiness;
use app\api\model\Attribute;
use app\api\model\Category;
use app\api\model\Images;
use app\api\model\Spec_goods;
use think\db\exception\DbEventException;
use think\facade\Db;
use think\Request;

class Goods
{

    public function index()
    {
        $page = \request()->get('page', "1");
        $data = \app\api\model\Goods::with('category')->paginate(10);
//        foreach ($data as $val){
//            $val["cate_name"]= "手机";
//        }
        return success($data);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 商品添加
     * @param Request $request
     * @return \think\response\Json|void
     * @throws \think\exception\PDOException
     */
    public function save(Request $request)
    {
        $data = [
            'goods_name' => 'OPPO 99',
            'goods_price' => '8900',
            'suppliers_id' => 0,
            'goods_introduce' => 'iphone iphonex',
            'goods_logo' => '/uploads/goods/20190101/afdngrijskfsfa.jpg',
            'goods_images' => [
                '666666',
                '777777777777',
                '88888888888888',
            ],
            'cate_id' => '72',
            'brand_id' => '3',
            'type_id' => '16',
            'item' => [
                '18_21' => [
                    'value_ids' => '18_21',
                    'value_names' => '颜色：黑色；内存：64G',
                    'price' => '8900.00',
                    'cost_price' => '5000.00',
                    'store_count' => 100
                ],
                '18_22' => [
                    'value_ids' => '18_22',
                    'value_names' => '颜色：黑色；内存：128G',
                    'price' => '9000.00',
                    'cost_price' => '5000.00',
                    'store_count' => 50
                ]
            ],
            'attr' => [
                '7' => ['id' => '7', 'attr_name' => '毛重', 'attr_values' => '150g'],
                '8' => ['id' => '8', 'attr_name' => '产地', 'attr_values' => '国产'],
                '9' => ['id' => '9', 'attr_name' => '材料', 'attr_values' => '塑胶'],
            ]
        ];
        // 开启事务
        Db::startTrans();
        try {
            // 添加商品
            $goods_id = GoodsBusiness::addGoods($data);
            //添加图片
            GoodsBusiness::addImage($data,$goods_id);
            // 添加商品规格值
            GoodsBusiness::addSpecGoods($data,$goods_id);
            // 添加商品属性
            GoodsBusiness::addAttr($data);
            // 提交事务
            Db::commit();
            return success('添加成功');
        }catch (DbEventException $exception){
            // 回滚事务
            Db::rollback();
            return fail($exception->getMessage());
        }


    }

    /**
     * 查看详情
     * @param $id
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function read($id)
    {
        if (is_numeric($id) || $id == 0) {
            return fail('id值有问题');
        }
        $data = \app\api\model\Goods::with(['category', 'brand', 'images', 'type', 'type.spec', 'type.spec.specValue', 'attrs', 'specGoods'])->where('id', $id)->select();

        return success($data);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $data = [
            'goods_name' => '小米18',
            'goods_price' => '9900',
            'suppliers_id' => 0,
            'goods_introduce' => 'iphone iphonex',
            'goods_logo' => '/uploads/goods/20190101/afdngrijskfsfa.jpg',
            'goods_images' => [
                '111111111',
                '2222222222222',
                '3333333333333333333333',
            ],
            'cate_id' => '72',
            'brand_id' => '3',
            'type_id' => '16',
            'item' => [
                '18_21' => [
                    'value_ids' => '18_21',
                    'value_names' => '颜色：黑色；内存：64G',
                    'price' => '8900.00',
                    'cost_price' => '5000.00',
                    'store_count' => 100
                ],
                '18_22' => [
                    'value_ids' => '18_22',
                    'value_names' => '颜色：黑色；内存：128G',
                    'price' => '9000.00',
                    'cost_price' => '5000.00',
                    'store_count' => 50
                ]
            ],
            'attr' => [
                '7' => ['id' => '7', 'attr_name' => '毛重', 'attr_values' => '150g'],
                '8' => ['id' => '8', 'attr_name' => '产地', 'attr_values' => '国产'],
                '9' => ['id' => '9', 'attr_name' => 'ee', 'attr_values' => 'ee'],

            ]
        ];
        // 启动事务
        Db::startTrans();
        try {
            // 修改商品
            \app\api\model\Goods::updateGoods($data,$id);
            // 修改商品图片
            Images::delImage($id);
            if (isset($data['goods_images'])){
                GoodsBusiness::addImage($data,$id);
            }
            // 修改商品属性
            GoodsBusiness::addAttr($data);
            // 修改商品规格值
            Spec_goods::delSpecGoods($id);
            if (isset($data['item'])){
                GoodsBusiness::addSpecGoods($data,$id);
            }
            // 提交事务
            Db::commit();
            return success('修改成功');
        }catch (DbEventException $exception){
            // 回滚事务
            Db::rollback();
            return fail($exception->getMessage());

        }

    }



    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $res = \app\api\model\Goods::delGoods($id);

        return success($res);
    }
}
